import { defineStore } from 'pinia'
import { default as LoginApi } from "@/api/login";
import { res } from '@/api/serve';
import router from '@/router'
export const userInfoStore = defineStore('userInfo', {
  state() {
    return {
      token: localStorage.getItem('token') || '',
      userInfo: {
        phone: '',
        password: ''
      },
      name: "",
      headImg: ""
    }
  },
  actions: {
    // 登录
    async SignIn(userInfo: any) {
      try {
        //获取token
        const result: any = await LoginApi.reqUserLogin(userInfo)
        this.token = result.token
        localStorage.setItem('token', result.token)
        return '成功'
      } catch (error) {
        return Promise.reject('失败')
      }
    },
    // 获取用户信息
    async getUserInfoData() {
      const result: any = await LoginApi.getUserInfoApi()
      this.name = result.name
      this.headImg = result.headImg
      // console.log(result);
    },
    // 退出登录
    async logoutlogin() {
      const result = await LoginApi.logout()
      // console.log(result);
    },
    // 重置用户信息
    reset() {
      this.token = ''
      this.userInfo = {
        phone: '',
        password: ''
      }
    }
  },
  getters: {

  }
})